import React from 'react';

import { mount } from 'react-mounter';
import { Provider } from 'react-redux';
import { useDeps, composeAll } from 'mantra-core';

export default (layoutClass, regions, options = {}) => {
  const content = regions.content;
  if (typeof content !== 'function') {
    throw new Error('The content should be a function which returns the inner component');
  }

  const mapper = (context) => ({
    store: context.ReduxStore
  });

  regions.content = () => (React.createElement(
    composeAll(
      useDeps(mapper)
    )(
      ({store}) => (
        <Provider store={store}>
          {content()}
        </Provider>
      )
    )
  ));

  return mount(layoutClass, regions, options);
};
